package com.jpcc.vw.onlineRadio.utils;


import com.jpcc.vw.onlineRadio.application.MusicApplication;
import com.jpcc.vw.onlineRadio.model.IRadio;
import com.jpcc.vw.onlineRadio.model.bean.AlbumBrowseBean;
import com.jpcc.vw.onlineRadio.model.bean.RecentAlbumRadioTrackBean;
import com.jpcc.vw.onlineRadio.model.bean.Track;
import com.jpcc.vw.onlineRadio.model.impl.RadioImpl;

import rx.Observable;
import rx.Subscriber;
import rx.functions.Func2;

public class NextFavTrackUtils {

    public interface GetNextTrack {
        void OnGetNextTrack(RecentAlbumRadioTrackBean result);
    }

    private static IRadio radioModel = new RadioImpl();

    public static void GetNextTrack(RecentAlbumRadioTrackBean source, GetNextTrack call) {
//        RecentAlbumRadioTrackBean value = new RecentAlbumRadioTrackBean();
        if ("album".equals(source.getType())) {
            Observable.zip(radioModel.GetAlbumBrowse(source.getAlbumId(), source.getTrackPageIndex()), radioModel.GetAlbumBrowse(source.getAlbumId(), source.getTrackPageIndex() + 1), new Func2<AlbumBrowseBean, AlbumBrowseBean, RecentAlbumRadioTrackBean>() {
                @Override
                public RecentAlbumRadioTrackBean call(AlbumBrowseBean albumBrowseBean, AlbumBrowseBean albumBrowseBean2) {
//                    if (albumBrowseBean2.getData().getTracks() != null && albumBrowseBean2.getData().getTracks().size() > 0) {
//                        albumBrowseBean.getData().getTracks().addAll(albumBrowseBean2.getData().getTracks());
//                    }
//                    return albumBrowseBean;
                    RecentAlbumRadioTrackBean value = new RecentAlbumRadioTrackBean();
                    for (int index = 0; index < albumBrowseBean.getData().getTracks().size(); index++) {
                        if ((albumBrowseBean.getData().getTracks().get(index).getId() == source.getTrackId())) {
                            if (index == albumBrowseBean.getData().getTracks().size() - 1) {
                                value.setStatus(-1);
                            } else {
                                value = new RecentAlbumRadioTrackBean();
                                Track nextTrack = albumBrowseBean.getData().getTracks().get(index + 1);
                                value.setUserId(MusicApplication.GetUserId());
                                value.setType("album");
                                value.setAlbumId(albumBrowseBean.getData().getAlbumId());
                                value.setAlbumName(albumBrowseBean.getData().getAlbumTitle());
                                value.setAuthor(nextTrack.getAnnouncer().getNickname());
                                value.setTrackId(nextTrack.getId());
                                value.setFrom(0);  //Service中会重新设置这个值
                                value.setLive(false);
                                value.setTrackPageIndex(source.getTrackPageIndex());
                                value.setCover("http://qukufile2.qianqian.com/data2/pic/265dfb17cb17cc87933d712fce7faf14/597854179/597854179.jpg@s_1,w_150,h_150");
                                value.setTrackUrl("http://zhangmenshiting.qianqian.com/data2/music/f9406076087e38e43fd6bca02a2881ea/597856412/597856412.mp3?xcode=aa21536376c3941e5342a3548728ab76");
                                value.setDuration(267000);
                            }
                        }
                    }
                    //上一页的最后一个
                    if (value.getStatus() == -1 && albumBrowseBean2.getData().getTracks().size() > 0) {
                        value = new RecentAlbumRadioTrackBean();
                        Track nextTrack = albumBrowseBean2.getData().getTracks().get(0);
                        value.setUserId(MusicApplication.GetUserId());
                        value.setStatus(0);
                        value.setType("album");
                        value.setAlbumId(albumBrowseBean.getData().getAlbumId());
                        value.setAlbumName(albumBrowseBean.getData().getAlbumTitle());
                        value.setAuthor(nextTrack.getAnnouncer().getNickname());
                        value.setTrackId(nextTrack.getId());
                        value.setFrom(0);//Service中会重新设置这个值
                        value.setLive(false);
                        value.setTrackPageIndex(source.getTrackPageIndex() + 1);
                        value.setCover("http://qukufile2.qianqian.com/data2/pic/265dfb17cb17cc87933d712fce7faf14/597854179/597854179.jpg@s_1,w_150,h_150");
                        value.setTrackUrl("http://zhangmenshiting.qianqian.com/data2/music/f9406076087e38e43fd6bca02a2881ea/597856412/597856412.mp3?xcode=aa21536376c3941e5342a3548728ab76");
                        value.setDuration(267000);
                    }
                    value.setStatus(-1);
                    return value;
                }
            }).subscribe(new Subscriber<RecentAlbumRadioTrackBean>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onNext(RecentAlbumRadioTrackBean recentAlbumRadioTrackBean) {
                    if (call != null) {
                        call.OnGetNextTrack(recentAlbumRadioTrackBean);
                    }
                }
            });
//                    .flatMap(new Func1<RecentAlbumRadioTrackBean, Observable<AlbumBrowseBean>>() {}

//                @Override
//                public Observable<AlbumBrowseBean> call(RecentAlbumRadioTrackBean recentAlbumRadioTrackBean) {
//                    if (recentAlbumRadioTrackBean == null) {
//                        radioModel.GetAlbumRadioFavourite().flatMap(new Func1<AlbumFavouriteBean, Observable<RecentAlbumRadioTrackBean>>() {
//                            @Override
//                            public Observable<RecentAlbumRadioTrackBean> call(AlbumFavouriteBean albumFavouriteBean) {
//                                List<CommonFavouritePaiedHistoryEntity> radio = new ArrayList<>();
//                                radio.clear();
//                                radio.addAll(albumFavouriteBean.getData().getRadios());
//                                for (int index = 0; index < radio.size(); index++) {
//                                    if (radio.get(index).getId() == recentAlbumRadioTrackBean.getAlbumId()) {
//                                        if (index == radio.size() - 1) {
//                                            Observable.just(null);
//                                        } else {
//                                            radioModel.GetAlbumBrowse(radio.get(index + 1).getId(), 1);
//                                        }
//                                    }
//                                }
//                                return null;
//                            }
//                        });
//                    } else {
//                        return Observable.just(recentAlbumRadioTrackBean);
//                    }
//                }
//            })
        }
    }
}
